//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hw.one.collect.client;

import cn.com.dekn.client.webservice.vehicleinfo.vo.ArrayOfCarPositionInfoVO;
import cn.com.dekn.client.webservice.vehicleinfo.vo.CarPositionInfoVO;
import cn.csg.soa.ycfz.GetSpeedAlarm;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.collect.movingsource.slagcar.constans.SlagCarConstants;
import com.hw.one.collect.movingsource.slagcar.po.PatrolDataPo;
import com.hw.one.collect.movingsource.slagcar.po.PatrolDevicePo;
import com.mlb.analyse.webservice.DmDataInfoVO;
import com.mlb.analyse.webservice.GetPmData;
import com.mlb.analyse.webservice.GetRealTimePmData;
import org.apache.cxf.common.jaxb.JAXBUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

public class ClientFactory {
    private static final Logger log = Logger.getLogger(ClientFactory.class);
    private static Client clientRaiseDust;
    private static Client clientSlagCar;
    private static ClientFactory clientFactory = new ClientFactory();

    private static String authKey = "64A8AE6702C7B3812B34B1DDDB28D3B9436EA4D2598C5DA778F9D5BCD02FEDEE07416CB8A95F385849B8F4F4AE8C7EB791349573C6C1171D7AC2C30997C88F2B7A29146D053F22187FB05F40C56D7EA34E2046D8315B5A213707DECC6A02685D59B081DCD751380D434822B9686E382A1E243B2634E52FF4257D5BC2763E7F308CD0020305877E9C7CBB63CCF815A91C";
    private static String PSInfoService = "http://222.143.40.248:8081/psinfoservice/PSInfoService.asmx?WSDL&serviceName=PSInfoService&fromRegionCode=410100000&toRegionCode=410100000&authKey=" + authKey;
    private static String AutoMonitorFacWebServices = "http://222.143.40.248:81/AutoMonitorWebSite/WebService/AutoMonitorFacWebServices.asmx?WSDL&serviceName=AutoMonitorFacWebServices&fromRegionCode=320000000&toRegionCode=320000000&authKey=" + authKey;
    private static String RunningService = "http://222.143.40.248:81/psinfoservice/RunningService.asmx?WSDL&serviceName=RunningService&fromRegionCode=320000000&toRegionCode=320000000&authKey=" + authKey;

    private static String TestMobile = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";

    private static String ZhaTuCar = "http://27.50.131.39:8686/muckmonitor/services/VehicleInfoService?wsdl";

    public static String raiseDustType = "raiseDust";
    public static String slagCarType = "slagCar";

    private ClientFactory() {
    }

    public static ClientFactory newInstance() {
        return clientFactory;
    }

    /**
     * 创建客户端
     *
     * @param serviceUrl
     * @return
     */
    private Client createClient(String serviceUrl) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client cxfClient = dcf.createClient(serviceUrl);
        HTTPConduit conduit = (HTTPConduit) cxfClient.getConduit();
        HTTPClientPolicy clientPolicy = new HTTPClientPolicy();
        clientPolicy.setConnectionTimeout(60000L);
        clientPolicy.setReceiveTimeout(60000L);
        conduit.setClient(clientPolicy);
        log.debug("客户端实例：" + cxfClient.toString());
        return cxfClient;
    }

    public Client getClient(String serviceUrl, String serviceType) {
        // 创建市建委扬尘的接口客户端
        if (raiseDustType.equals(serviceType)) {
            if (clientRaiseDust == null) {
                clientRaiseDust = createClient(serviceUrl);
            }
            return clientRaiseDust;
        }
        // 创建城管局渣土车的接口客户端
        if (slagCarType.equals(serviceType)) {
            if (clientSlagCar == null) {
                clientSlagCar = createClient(serviceUrl);
            }
            return clientSlagCar;
        }
        return null;
    }


    public static void getClient2() throws Exception {
        Client client = ClientFactory.newInstance().getClient("http://123.15.58.210:6001/dataInfoService?wsdl", raiseDustType);

        GetRealTimePmData realTimePmData = new GetRealTimePmData();
        realTimePmData.setDistrictCode("410122");
        Object[] e = client.invoke("getRealTimePmDataInfo", realTimePmData);
        if (e[0] instanceof List) {
            List<Object> list = (List<Object>) e[0];
            System.out.println("e = " + list.size());
            for (Object dmDataInfoVO : list) {
                if (dmDataInfoVO instanceof DmDataInfoVO) {
//                    System.out.println( ((DmDataInfoVO) dmDataInfoVO).getMonitorTime() + " " + ((DmDataInfoVO) dmDataInfoVO).getReportTime() + ((DmDataInfoVO) dmDataInfoVO).getBSName() + " " );
                    System.out.println(dmDataInfoVO.toString());
                }
            }
        }
        System.out.println("e = " + e[0]);

    }

    public static void getClient3() throws Exception {
        Client client = ClientFactory.newInstance().getClient(PSInfoService, raiseDustType);

        Object[] e = client.invoke("GetPSInfo", "");
        if (e[0] instanceof List) {
            List<Object> list = (List<Object>) e[0];
            System.out.println("e = " + list.size());
            for (Object dmDataInfoVO : list) {
                if (dmDataInfoVO instanceof DmDataInfoVO) {
//                    System.out.println( ((DmDataInfoVO) dmDataInfoVO).getMonitorTime() + " " + ((DmDataInfoVO) dmDataInfoVO).getReportTime() + ((DmDataInfoVO) dmDataInfoVO).getBSName() + " " );
                    System.out.println(dmDataInfoVO.toString());
                }
            }
        }
        System.out.println("e = " + e[0]);

    }

    public static void getClient4() throws Exception {
        SlagCarConstants.getRegionIdWithName();
        Map<String, String[]> regionIdWithOtherCode = SlagCarConstants.regionIdWithOtherCode;
        log.info("-----------------------------请求链接---------------------");
        log.info(SlagCarConstants.SlagCar_URI);
        log.info("----------------------------------------------------------");

        // 创建客户端
        Client client = ClientFactory.newInstance().getClient(SlagCarConstants.SlagCar_URI, ClientFactory.slagCarType);
        List<PatrolDevicePo> patrolDeviceList = new ArrayList<>();
        List<PatrolDataPo> patrolDataList = new ArrayList<>();
        for (String key : regionIdWithOtherCode.keySet()) {
            try {
                log.info("-------------------当前请求的城市：" + regionIdWithOtherCode.get(key)[0] + "-----------");

                // 构建查询参数
                Object[] e = client.invoke(SlagCarConstants.getVehicleInfo, key);

                if (e[0] instanceof CarPositionInfoVO) {
                    CarPositionInfoVO carPositionInfoVO = (CarPositionInfoVO) e[0];
                } else if (e[0] instanceof ArrayOfCarPositionInfoVO) {
                    ArrayOfCarPositionInfoVO arrayOfCarPositionInfoVO = (ArrayOfCarPositionInfoVO) e[0];
                    for (CarPositionInfoVO carPositionInfoVO : arrayOfCarPositionInfoVO.getCarPositionInfoVO()) {

                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


    public static void getClient5() throws Exception {
        SlagCarConstants.getRegionIdWithName();
        Map<String, String[]> regionIdWithOtherCode = SlagCarConstants.regionIdWithOtherCode;
        log.info("-----------------------------请求链接---------------------");
        log.info(SlagCarConstants.SlagCar_URI);
        log.info("----------------------------------------------------------");

        // 创建客户端
        Client client = ClientFactory.newInstance().getClient(SlagCarConstants.SlagCar_URI, ClientFactory.slagCarType);
        List<PatrolDevicePo> patrolDeviceList = new ArrayList<>();
        List<PatrolDataPo> patrolDataList = new ArrayList<>();
        for (String key : regionIdWithOtherCode.keySet()) {
            try {
                log.info("-------------------当前请求的城市：" + regionIdWithOtherCode.get(key)[0] + "-----------");
                GetSpeedAlarm getSpeedAlarm = new GetSpeedAlarm();
                getSpeedAlarm.setDistrictCode(key);
                getSpeedAlarm.setBeginTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

                // 构建查询参数
                Object[] e = client.invoke(SlagCarConstants.getSpeedAlarm, key, "", "");

                if (e[0] instanceof CarPositionInfoVO) {
                    CarPositionInfoVO carPositionInfoVO = (CarPositionInfoVO) e[0];
                } else if (e[0] instanceof ArrayOfCarPositionInfoVO) {
                    ArrayOfCarPositionInfoVO arrayOfCarPositionInfoVO = (ArrayOfCarPositionInfoVO) e[0];
                    for (CarPositionInfoVO carPositionInfoVO : arrayOfCarPositionInfoVO.getCarPositionInfoVO()) {

                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {

        try {
            getClient2();
//            Map<String,String> headers = new HashMap<>();
//            Map<String,String> params = new HashMap<>();
//            params.put("beginTime", "2017-08-21 15:05:40");
//            params.put("endTime", "2017-08-21 17:05:40");
//            params.put("serialNumber", "AT1501G0054S");
//
//            Map<String,String> resultMap = HttpClient.sendPost("http://222.143.40.251:8089/QuerySpaData", headers, params);
//            if ("200".equals(resultMap.get("statusCode"))) {
//                System.out.println("" + resultMap.get("result"));
//            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

}
